//
//  TJSecurityCenterController.m
//  Teleport
//
//  Created by xtjun on 2020/6/13.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJSecurityCenterController.h"
#import "TJSettingMailController.h"
@interface TJSecurityCenterController ()

@property (strong, nonatomic) NSMutableArray *datas;


@end

@implementation TJSecurityCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 60;
    [self.tableView registerClass:TJStyle1Cell.self forCellReuseIdentifier:style1Cell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJStyle1Cell *cell = [tableView dequeueReusableCellWithIdentifier:style1Cell forIndexPath:indexPath];
    TJCellStyleModel *model = self.datas[indexPath.row];
    cell.model = model;
    cell.selectionStyle = model.hasArrow.boolValue ?  UITableViewCellSelectionStyleDefault : UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TJCellStyleModel *model = self.datas[indexPath.row];
    if ([model.identification isEqualToString:@"moneyPassword"]) {
        [self.navigationController pushViewControllerWithName:@"TJSettingMoneyPasswordController" title:model.text animated:YES];
        return;
    }
    if ([model.identification isEqualToString:@"mail"]) {
        
        TJSettingMailController *mailVC = [TJSettingMailController new];
        weakify(self)
        mailVC.complete = ^(NSString * _Nonnull mail) {
            NSDictionary *dic =  @{
                           @"text": @"邮箱绑定".localizedString,
                           @"detailText": mail,
                           @"identification": @"mail",
                           @"hasArrow": @(1),
            };
            [weakself.datas replaceObjectAtIndex:1 withObject:dic];
        };
        [self.navigationController pushViewController:mailVC animated:YES];
        return;
    }
    
}

- (NSMutableArray *)datas {
    if (!_datas) {
        NSMutableArray *arr = @[
            @{
                @"text": @"手机绑定".localizedString,
                @"detailText": TJUserInfo.readInfo.phone,
                @"identification": @"phone",
                @"hasArrow": @(0),
            },
            @{
                @"text": @"邮箱绑定".localizedString,
                @"detailText": TJUserInfo.readInfo.mail,
                @"identification": @"mail",
                @"hasArrow": @(1),
            },
            @{
                @"text": @"资金密码".localizedString,
                @"detailText": TJUserInfo.readInfo.payPassword !=nil ? @"修改".localizedString : @"设置".localizedString,
                @"identification": @"moneyPassword",
                @"hasArrow": @(1),
            }
        ].mutableCopy;
        
        _datas = [TJCellStyleModel mj_objectArrayWithKeyValuesArray:arr];
    }
    return _datas;
    
}

@end

